package com.yx.cloud.utils;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.yx.cloud.R;

/**
 * 动画
 */
public class AnimationtUtils {

    private final static String TAG = "AnimationtUtils";

    private static Animation rotateAnim, rotateAnimRight;
    private static AlphaAnimation alphaAnimation;


    /**
     * 渐变动画
     *
     * @param view
     */
    public static void startAlphaAnimation(View view, long time) {
        alphaAnimation = new AlphaAnimation(0.2f, 1.0f);//渐变度从0到1
        alphaAnimation.setDuration(time);//动画持续时间：2000毫秒
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        view.startAnimation(alphaAnimation);
    }

    /**
     * 停止 渐变动画
     */
    public static void stopAlphaAnimation() {
        if (alphaAnimation != null) {
            alphaAnimation.cancel();
        }
    }

    /**
     * 旋转动画
     *
     * @param view
     * @param time
     */
    public static void setRotateAnimation(ImageView view, long time) {
        Log.d(TAG, "setRotateAnimation ==2: ");
        if (rotateAnim == null) {
            Log.d(TAG, "setRotateAnimation ==3: ");
            rotateAnim = new RotateAnimation(360f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//            LinearInterpolator lin = new LinearInterpolator();
//            rotateAnim.setInterpolator(lin);
//            rotateAnim.setDuration(time);//设置动画持续周期
//            rotateAnim.setRepeatCount(-1);//设置重复次数
//            rotateAnim.setRepeatMode(Animation.RESTART);
//            rotateAnim.setFillAfter(true);//动画执行完后是否停留在执行完的状态
//            rotateAnim.setStartOffset(10);//执行前的等待时间
//            rotateAnim = AnimationUtils.loadAnimation(context, R.anim.animation_rotate);
            rotateAnim.setDuration(time);//设置动画持续周期
            rotateAnim.setFillAfter(true);
            rotateAnim.setInterpolator(new LinearInterpolator());
            rotateAnim.setRepeatCount(ObjectAnimator.INFINITE);//设置重复次数
            view.setAnimation(rotateAnim);
        } else {
            Log.d(TAG, "setRotateAnimation ==4: ");
            view.startAnimation(rotateAnim);
        }
    }

    /**
     * 旋转动画
     *
     * @param view
     * @param time
     */
    public static void setRotateAnimationRight(ImageView view, long time) {
        Log.d(TAG, "setRotateAnimation Right ==2: ");
        if (rotateAnimRight == null) {
            Log.d(TAG, "setRotateAnimation Right ==3: ");
            rotateAnimRight = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimRight.setDuration(time);//设置动画持续周期
            rotateAnimRight.setFillAfter(true);
            rotateAnimRight.setInterpolator(new LinearInterpolator());
            rotateAnimRight.setRepeatCount(ObjectAnimator.INFINITE);//设置重复次数
            view.setAnimation(rotateAnimRight);
        } else {
            Log.d(TAG, "setRotateAnimation Right ==4: ");
            view.startAnimation(rotateAnimRight);
        }
    }

    /**
     * 停止 旋转动画
     */
    public static void stopRotateAnimation() {
        if (rotateAnim != null) {
            rotateAnim.cancel();
            rotateAnim = null;
        }
    }

    public static void stopRotateAnimationRight() {
        if (rotateAnimRight != null) {
            rotateAnimRight.cancel();
            rotateAnimRight = null;
        }
    }

    /**
     * 平移+旋转动画
     *
     * @param view
     */

    public static void startTranslateAnimation(ImageView view, Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        float width = dm.widthPixels;
        float x = view.getX();
        float trx = width - x;
        AnimationSet animationSet = new AnimationSet(false);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                // X轴的开始位置
                Animation.ABSOLUTE, 0f,
                // X轴的结束位置
                Animation.ABSOLUTE, trx - 300,
                // Y轴的开始位置
                Animation.ABSOLUTE, 0f,
                // Y轴的结束位置
                Animation.ABSOLUTE, 0f);
        translateAnimation.setDuration(3000);
        translateAnimation.setRepeatCount(-1); // 设置动画重复次数
        translateAnimation.setRepeatMode(Animation.REVERSE);
//        translateAnimation.setRepeatMode(Animation.RESTART); //重新从头执行
//        translateAnimation.setRepeatMode(Animation.REVERSE); //反方向执行
        animationSet.addAnimation(translateAnimation);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                float scaleX = view.getScaleX();
                if (scaleX == -1) {
                    //设置图片翻转
                    view.setScaleX(1);
                } else {
                    view.setScaleX(-1);
                }
            }
        });

        view.startAnimation(animationSet);
    }

    /**
     * 获取系统是否关系了动画
     *
     * @param context
     * @return
     */
    public static boolean isPowerSaveMode(Context context) {
        PowerManager mPowerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (mPowerManager.isPowerSaveMode()) {
                return true;
            }
        }
        return isAnimatorDurationScale(context);
    }

    public static boolean isAnimatorDurationScale(Context context) {
        float animatorDurationScale = Settings.Global.getFloat(context.getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE, 1f);

        if (animatorDurationScale == 0) {
            return true;
        }
        return false;
    }

}
